<?php
/*
下面几行注释是启动memcache的bash脚本
#!/bin/sh
port=11211
while test $port -lt 11223 
do
    /usr/local/bin/memcached -p $port &
    port=$(($port+1))
done
*/
$host='127.0.0.1';
$test_num=10000;
$ports=range(11211,11220);
$ports_add=range(11221,11222);
$strategies=array('consistent','standard');
$hashes=array('crc32','fnv');
echo '测试数目：',$test_num,"\n-------\n";
foreach ($strategies as $strategy) {
    foreach ($hashes as $hash) {
        ini_set('memcache.hash_strategy',$strategy);
        ini_set('memcache.hash_function',$hash);
        echo '哈希策略：',$strategy,"\n";
        echo '哈希函数：',$hash,"\n";
        $mc=new Memcache;
        foreach ($ports as $port) {
            $mc->addServer($host,$port);
        }

        $mc->flush();
        for ($i=0;$i<$test_num;$i++) {
            $mc->set($i,$i);
        }

        echo "分布：\n";
        foreach ($ports as $port) {
            $single_mc=new Memcache();
            $single_mc->connect($host,$port);
            $get_num=0;
            for ($i=0;$i<$test_num;$i++) {
                if ($single_mc->get($i)) {
                    $get_num++;
                }
            }
            echo ' port:',$port,':',round($get_num/$test_num,4)*100,"%\n";
            unset($single_mc);
        }

        foreach ($ports_add as $port) {
            $mc->addServer($host,$port);
        }

        $get_num=0;
        for ($i=0;$i<$test_num;$i++) {
            if ($mc->get($i)) {
                $get_num++;
            }
        }

        echo '存活率:';
        echo round($get_num/$test_num,4)*100,'%';
        echo "\n-------\n";
        unset($mc);
    }
}
